package metier;
import java.util.ArrayList;
import java.util.List;


import modele.Client;
import modele.Module;
import modele.Moduleprofil;
import modele.Profil;
import modele.Taxe;

import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.ArticleDAO;
import dao.CatcomptaDAO;
import dao.ClientDAO;
import dao.ComptegDAO;
import dao.ModuleDAO;
import dao.ModuleprofilDAO;
import dao.ProfilDAO;
import dao.TaxeDAO;
import dao.UniteventeDAO;
import persistence.BaseDAO;
import utils.Util;


public class ParamManager {
	
	
	public static List getAllCompteg()	throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = ComptegDAO.getListCompteg("from Compteg",session);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getAllCatCompta()	throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = CatcomptaDAO.getListCatcompta("from Catcompta",session);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getAllProfils()	throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = ProfilDAO.getListProfil("from Profil",session);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getAllModules()	throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = ModuleDAO.getListModule("from Module",session);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getProfils(int start, int limit) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = ProfilDAO.getListProfil("from Profil",session,start,limit);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getUniteventes(int start, int limit) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = UniteventeDAO.getListUnitevente("from Unitevente",session,start,limit);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getTaxes(int start, int limit) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = TaxeDAO.getListTaxe("from Taxe",session,start,limit);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List ajouterTaxes(Object data) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			List<Taxe> lToAdd =Util.getTaxeFromRequest(data);
			for (Taxe taxe : lToAdd) {
				TaxeDAO.ajouterTaxe(taxe, session);
			}
			tx.commit();
			session.close();
			return lToAdd;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}

	public static List modfierTaxes(Object data)throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			List<Taxe> lToUpdate =Util.getTaxeFromRequest(data);
			for (Taxe taxe : lToUpdate) {
				TaxeDAO.modifierTaxe(taxe, session);
			}
			tx.commit();
			session.close();
			return lToUpdate;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}

	public static boolean supprimerTaxes(Object data) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			//it is an array - have to cast to array object
			if (data.toString().indexOf('[') > -1){
				List<Integer> deleteTaxes = Util.getListIdFromJSON(data);
				for (Integer id : deleteTaxes){
					TaxeDAO.SupprimerTaxe(TaxeDAO.getTaxe(id, session), session);
				}
			} else { //it is only one object - cast to object/bean
				Integer id = Integer.parseInt(data.toString());
				TaxeDAO.SupprimerTaxe(TaxeDAO.getTaxe(id, session), session);
			}
			tx.commit();
			session.close();
			return true;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getCatcomptas(int start, int limit) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List list  = CatcomptaDAO.getListCatcompta("from Catcompta",session,start,limit);
			session.close();
			return list;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	public static List getModuleprofils(int idProfil) throws Exception {
		Session session = BaseDAO.getInstance().openSession();
		try {
			List<Module> m= ModuleDAO.getListModule("from Module", session);
			List<Moduleprofil> c = new ArrayList<Moduleprofil>();
			for (Module module : m)
			{
				List mp = ModuleprofilDAO.getListModuleprofil("from Moduleprofil mp where mp.idProfil="+idProfil+" AND mp.idModule="+module.getIdModule(),session);
				if(mp.size() > 0)
					c.add((Moduleprofil) mp.get(0));
				else
				{
					Moduleprofil nmp=new Moduleprofil();
					nmp.setIdModuleprofil(0);
					nmp.setIdModule(module.getIdModule());
					nmp.setIdProfil(idProfil);
					nmp.setC(0);
					nmp.setR(0);
					nmp.setU(0);
					nmp.setD(0);
					c.add(nmp);
				}
			}
			return c;
		} catch (Exception e) {
			e.printStackTrace();			
			throw new Exception(e.getMessage());
		}
	}
	
}
